package headline

import (
	"api/global"
	"api/model"
	"api/types"
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	pb "rpc/api/video"
)

// 视频列表
func VideoList(c *gin.Context) {
	var data types.GetVideoListRequest
	if err := c.ShouldBindJSON(&data); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	list := model.VideoList(data)
	if len(list) == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "列表查询为空",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "查询列表成功",
		"data":    list,
	})
}

// 视频搜索（es）
func VideoSearch(c *gin.Context) {
	var data types.VideoSearch
	if err := c.ShouldBindJSON(&data); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	search := model.VideoSearch(data)

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "视频搜索成功",
		"data":    search,
	})
}

// 视频详情
func VideoDetail(c *gin.Context) {
	var data types.VideoDetail
	if err := c.ShouldBindJSON(&data); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	search := model.VideoDetail(data)
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "视频详情查询成功",
		"data":    search,
	})
}

// 视频发布
func VideoPublish(c *gin.Context) {
	var data types.VideoPublishRequest
	if err := c.ShouldBindJSON(&data); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	// 3.组装请求参数// 4. 调用接口
	response, err := global.UserServiceClient.VideoPublish(context.Background(), &pb.VideoPublishRequest{
		VideoTitle:    data.VideoTitle,
		VideoSubTitle: data.VideoSubTitle,
		VideoAddress:  data.VideoAddress,
		VideoViews:    data.VideoViews,
		VideoType:     data.VideoType,
		VideoRegion:   data.VideoRegion,
		VideoImage:    data.VideoImage,
	})
	if err != nil {
		fmt.Println("响应异常  %s\n", err)
		return
	}
	fmt.Printf("响应结果： %v\n", response)
	if response.VideoID == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "视频发布失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "视频发布成功",
		"videoId": response.VideoID,
	})

}
